package com.turing.post.transaction.impl;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Date;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.transaction.util.MchChkAccCtl;
import com.turing.post.transaction.util.MchChkAccCtlDao;
import com.turing.post.util.Config;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;

@Stateless(mappedName = "transaction.impl.MchChkAccCtlDaoImpl")
@Remote
@Local
public class MchChkAccCtlDaoImpl implements MchChkAccCtlDao {
	/**
	 * 实体管理器
	 * 
	 * @see javax.persistence.EntityManager
	 */
	@PersistenceContext
	protected EntityManager _em;

	@Override
	public MchChkAccCtl getMchChkAccCtlByKey(String merchantNo, Date checkDate) {
		MchChkAccCtl mcac = new MchChkAccCtl();
		mcac.setMerchantNo(merchantNo);
		mcac.setCheckDate(checkDate);
		return _em.find(MchChkAccCtl.class, mcac);
	}

	@Override
	public void insert(MchChkAccCtl mcac) throws PCPSApplicationException {
		GeneralDao.insert(_em, mcac);
	}

	@Override
	public void update(MchChkAccCtl mcac) throws PCPSApplicationException {
		GeneralDao.update(_em, mcac);
	}

	/**
	 * 将逗号分隔的字符串转成 'a','b'的形式
	 * 
	 * @param paramlist
	 * @return
	 */
	private String generateSqlInCluause(String paramlist) {
		String[] inlist = paramlist.split(",");
		StringBuilder sb = new StringBuilder();
		boolean isfirst = true;
		for (String s : inlist) {
			if (!isfirst)
				sb.append(",");
			sb.append("'");
			sb.append(s);
			sb.append("'");
			isfirst = false;
		}
		return sb.toString();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MchChkAccCtl> getMchChkAccCtlListByCondition(Integer page,
			String issueCompanyNo, String merchantNo, String startDate,
			String endDate, String checkResult) {
		int maxResult = Config.PAGE_SIZE;
		int startPosition = maxResult * page;
		String sql = "select t.* from MchChkAccCtl t where t.merchantNo like ?1 and ";
		if (!PCPSUtil.isEmpty(merchantNo))
			sql += "t.merchantNo IN ( " + generateSqlInCluause(merchantNo)
					+ " ) and ";
		sql += "t.checkdate between to_date(?2,'yyyy-mm-dd') and to_date(?3,'yyyy-mm-dd') and "
				+ "(?4 is null or t.checkresult=?4) "
				+ "order by t.merchantno,t.checkdate desc";
		Query query = _em.createNativeQuery(sql, MchChkAccCtl.class)
				.setFirstResult(startPosition).setMaxResults(maxResult);
		query.setParameter(1, issueCompanyNo + "%");
		query.setParameter(2, startDate);
		query.setParameter(3, endDate);
		if (PCPSUtil.isEmpty(checkResult))
			query.setParameter(4, null);
		else
			query.setParameter(4, checkResult);
		return query.getResultList();
	}

	@Override
	public BigInteger getMchChkAccCtlTotalByCondition(String issueCompanyNo,
			String merchantNo, String startDate, String endDate,
			String checkResult) {
		String sql = "select nvl(count(*),0) count from MchChkAccCtl t where t.merchantNo like ?1 and ";
		if (!PCPSUtil.isEmpty(merchantNo))
			sql += "t.merchantNo IN ( " + generateSqlInCluause(merchantNo)
					+ " ) and ";
		sql += "t.checkdate between to_date(?2,'yyyy-mm-dd') and to_date(?3,'yyyy-mm-dd') and "
				+ "(?4 is null or t.checkresult=?4) "
				+ "order by t.merchantno,t.checkdate desc";
		Query query = _em.createNativeQuery(sql);
		query.setParameter(1, issueCompanyNo + "%");
		query.setParameter(2, startDate);
		query.setParameter(3, endDate);
		if (PCPSUtil.isEmpty(checkResult))
			query.setParameter(4, null);
		else
			query.setParameter(4, checkResult);
		return ((BigDecimal) query.getSingleResult()).toBigInteger();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Object[]> getMchChkAccCtlCount(String date) {
		String sql = "select m.checkresult,count(*) from MchChkAccCtl m where m.checkdate=to_date(?1,'yyyy-mm-dd') group by m.checkresult order by checkresult";
		Query query = _em.createNativeQuery(sql);
		query.setParameter(1, date);
		return query.getResultList();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MchChkAccCtl> getMchChkAccCtlCountDetail(String date,
			String checkResult) {
		String sql = "select t.* from MchChkAccCtl t where t.checkdate=to_date(?1,'yyyy-mm-dd') and t.checkresult=?2 order by t.merchantno";
		Query query = _em.createNativeQuery(sql, MchChkAccCtl.class);
		query.setParameter(1, date);
		query.setParameter(2, checkResult);
		return query.getResultList();
	}
}